package com.web.servlet.gameweb;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.web.servlet.bean.GameInfo;
import com.web.servlet.manager.GameServerMananger;
import com.web.servlet.manager.RedisManager;
/**
 * 1 房间列表
 * @author davie.yu
 *
 */
public class RoomListServlet extends HttpServlet {
	private static final long serialVersionUID = 1171269662365867452L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/plain");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		String uid =request.getParameter("uid");//用户ID
		String gid =request.getParameter("gid");//游戏ID
		String status =request.getParameter("s");//状态
		
		boolean result = true;
		
		
		JsonObject o = new JsonObject();
		o.addProperty("cmd", "roomlist");
		o.addProperty("uid", uid);
		o.addProperty("gid", gid);
		String re = HttpUtils.sentPost("http://112.74.133.57:2221/createroom/p", o.toString());
		
	    if(null != re){	
	    	List<String> parList = JSONArray.fromObject(re);
			System.out.println("RoomListServlet.doPost():" + re);
			
			Document document = DocumentHelper.createDocument();
	    	Element rootElement = document.addElement("RoomList");
	    	for(int i=0;i<parList.size();i++)
	    	{
	    		JSONObject json = JSONObject.fromObject(parList.get(i));
	    		Element ListElement = rootElement.addElement("list");
	        	Element ridElement = ListElement.addElement("rid"); //房号
	        	Element sElement = ListElement.addElement("s"); //底分
	        	Element seasonElement = ListElement.addElement("season"); //局数
	        	Element numElement = ListElement.addElement("num"); //当前人数
	        	Element maxElement = ListElement.addElement("max");//可容纳人数
	        	
	        	ridElement.setText(json.getString("rid"));
	        	sElement.setText(json.getString("s"));
	        	seasonElement.setText(json.getString("season"));
	        	numElement.setText(json.getString("num"));
	        	maxElement.setText(json.getString("max"));
	    	}
	    	out.print(document.asXML());
	    }
		out.flush();
		out.close();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}
	

	
	
	private void loginFail(PrintWriter out)
	{
		Document document = DocumentHelper.createDocument();
    	Element rootElement = document.addElement("user");
    	rootElement.addAttribute("st", "false");
    	rootElement.addAttribute("dc", "用户名或者密码不对");
    	out.print(document.asXML());
		
	}

}
